<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RelationshipHandler
 *
 * @author user
 */
class RelationshipHandler {
    //put your code here
     
    
    public function TeacherToSubject($teacherInCharge,$idSubject){
          $relateTeacherToSubjectQuery=mysql_query("INSERT INTO teacher_has_subject(idTeacher,idSubject)
                                                                                    VALUES (".$teacherInCharge.",".$idSubject.")")or die('Could not connect: ' . mysql_error());
    
          
          return $relateTeacherToSubjectQuery;
    }
    
    
    
    public function StudentsToSubject($SubjectId){
        
          $studentList=mysql_query("SELECT idStudent FROM student,subject,subject_yearlevel WHERE subject_yearlevel.subject_idSubject=".$SubjectId." AND subject_yearlevel.Level=student.studentlvl GROUP BY idStudent")
                                                                                            or  die('Could not connect: ' . mysql_error());
                                                                        
           
        
         
         while($row = mysql_fetch_array($studentList)){
                 extract($row);
                                            
                
                 
                $addedStudSubject=mysql_query("INSERT INTO student_has_subject(idStudent,idSubject) VALUES ('$idStudent','".$SubjectId."')")
                                or  die('Could not connect: ' . mysql_error());
         }
         
         
         return $studentList;
   }
   
     public function SubjectToStudent($StudentId){
         
       $studentLevel=mysql_query("SELECT studentlvl FROM student WHERE idStudent=".$StudentId)
               or  die('Could not connect: ' . mysql_error());
       
       $row1=mysql_fetch_array($studentLevel);
       extract($row1);
       
       $subjectList=mysql_query("SELECT idSubject FROM student,subject,subject_yearlevel WHERE subject_yearlevel.Level='".$studentlvl."' AND idSubject=subject_yearlevel.subject_idSubject AND idStudent=".$StudentId." GROUP BY idSubject")
               or  die('Could not connect: ' . mysql_error());
                          
       
                      while($row = mysql_fetch_array($subjectList)){
                                  extract($row);
                                 $addedSubjectStud=mysql_query("INSERT INTO student_has_subject(idStudent,idSubject) VALUES ('".$StudentId."',$idSubject)")
                                                                               or  die('Could not connect: ' . mysql_error());
                        }
     
                        
                        
       return $subjectList;                                                                
       
   }
    
   public function updateStudentstoSubject($subjectID){
       
       
        $deleteExistingStudentSubject=  mysql_query("DELETE FROM student_has_subject WHERE idSubject=".$subjectID);
            
        $studentList=mysql_query("SELECT idStudent FROM student,subject,subject_yearlevel WHERE subject_yearlevel.subject_idSubject=idSubject AND subject_yearlevel.Level=student.studentlvl GROUP BY idStudent")
                                                                                            or  die('2Could not connect: ' . mysql_error());
                                                                        
                                                                     
                                  while($row = mysql_fetch_array($studentList)){
                                                 extract($row);
                                                 
                                              
                                                                            
                                                $addedStudSubject=mysql_query("INSERT INTO student_has_subject(idStudent,idSubject) VALUES ('$idStudent','".$subjectID."')")
                                                         or  die('Could not connect: ' . mysql_error());
                                                                       
                                                                    
                                  }
                                  
      return $studentList;
                                         
   } 
   
   
    
   public function updateGrades($studentID){
       
        
        $updateGrades=mysql_query("SELECT gradecriterion_subject_idSubject FROM grade WHERE student_idStudent=".$studentID." AND
        gradecriterion_subject_idSubject NOT IN(SELECT idSubject FROM student_has_subject WHERE idStudent=".$studentID.") GROUP BY gradecriterion_subject_idSubject");
        
        while($row=mysql_fetch_array($updateGrades)){
            extract($row);
            
            $deleteEverything=mysql_query("DELETE FROM grade WHERE gradecriterion_subject_idSubject=".$gradecriterion_subject_idSubject." AND student_idStudent=".$studentID." AND archived=0");
        }
        
        return $updateGrades;
                                         
   } 
    
   
}

?>
